<?php
class MySQL_to_PHP{
	private $host;
	private $user;
	private $pass;
	private $dbname;
	private $connection;
	
	private $construct 	= true; 
	private $geters 	= true;
	private $seters 	= true;
	private $fetchData 	= true; 
	private $delete 	= true; 
	private $update 	= true; 
	private $insert 	= true;
	private $Order_Keys = true;
	
	private $classe_name 		= array( array( 'name', 'total_vars' ) );
	private $classe_variables 	= array( array( 'var_name', 'lenght', 'key' ) );
	
	private $num_of_tables 			= 0;
	private $num_of_total_variables = 0;
	
	private $m_strTableName;
	
	public $m_arrstrDataType;
	
	public function MySQL_to_PHP( $host, $user, $passoword, $dbname ) {
		
		$this->host 		= $host;
		$this->user 		= $user;
		$this->pass 		= $passoword;
		$this->dbname 		= $dbname;
		
		$this->m_arrstrDataType = array();
		
		$this->connection 	= new mysqli( $host, $user, $passoword, $dbname ); 
			if( $this->connection->connect_error ) {
				echo "Error connect!"; die;
			}
	}

	public function CreateClassesFiles() {
	$loop = $this->connection->query( "SHOW tables FROM " . $this->dbname . "" );	
		while( $row = $loop->fetch_row() ) {
			$this->classe_name[$this->num_of_tables]['name'] 		= $row[0];
			$this->classe_name[$this->num_of_tables]['total_vars'] 	= 0;
			
				$loop2 	= $this->connection->query( "SHOW columns FROM " . $row[0] );				
				if( false == $loop2 ) continue;
				$j 		= 0;
				while( $row2 = $loop2->fetch_row() ) {		
					$this->classe_variables[$j]['var_name'] = $row2[0];
					$this->classe_variables[$j]['lenght']	= $row2[1];
					$row2[3] == "PRI" ? $this->classe_variables[$j]['key'] = 1 : $this->classe_variables[$j]['key'] = 0;
					$this->num_of_total_variables += 1;
					$this->classe_name[$this->num_of_tables]['total_vars'] += 1;
					$j++;
				}	
			$this->CreatFiles();	
			$this->num_of_tables += 1;				
		}		
		$this->CreateDataBaseClass();	
	}
	
	private function CreatFiles() {
		
		$arrstrTableNameSlipt = explode( '_', $this->classe_name[$this->num_of_tables]['name']  );
		
		if( false == is_array( $arrstrTableNameSlipt ) ) return true;
		
		$this->m_strTableName = '';
		
		foreach( $arrstrTableNameSlipt as $strTableName ) {
			$this->m_strTableName .= ucfirst( $strTableName ); 
		}
	
		$file_in = "<?php\n";
		$file_in .= $this->Copyright();
		$file_in .= "\nrequire_once 'classes/DataBaseMysql.class.php';\n";
		$file_in .="\nClass Base" . $this->m_strTableName . " {\n\n";
	
		$file_in = $this->CreateVars( $file_in );

		$file_in .="	public function  __construct() {
				\$this->connection = new DataBaseMysql();
	}\n\n";
		
	/*	$file_in .="	public function ".$this->classe_name[$this->num_of_tables]['name']."() {
		\$this->connection = new DataBaseMysql();
	}";*/
		
		$file_in .= "	\n	/*******************************************************************************************
	******************************** get Functions *********************************************
	*******************************************************************************************/\n";
		if($this->geters) $file_in = $this->CreateFunctionGetters( $file_in );	
		
		$file_in .= "	\n	/*******************************************************************************************
	********************************* set Functions ********************************************
	*******************************************************************************************/\n";
		if($this->seters) $file_in = $this->CreateFunctionSetters( $file_in );
		
		$file_in .= "	\n	/*******************************************************************************************
	********************************* Insert Functions ********************************************
	*******************************************************************************************/\n";
	
		if($this->insert) $file_in = $this->CreateFunctionInsert( $file_in );
		
		$file_in .= "	\n	/*******************************************************************************************
	********************************* Update Functions ********************************************
	*******************************************************************************************/\n";
	
		if($this->update) $file_in = $this->CreateFunctionUpdate( $file_in );
		
		$file_in .= "	\n	/*******************************************************************************************
	********************************* Delete Functions ********************************************
	*******************************************************************************************/\n";
	
		if($this->delete) $file_in = $this->CreateFunctionDelete( $file_in );
		
		$file_in = $this->CreateFunctionFetchNextId( $file_in );
		
		$file_in .= "\n}";
		
		// call create Base Class function 
		$this->SaveBaseFile( "Base" . $this->m_strTableName . ".class.php", $file_in );
		
		// start code here to create new file as for the pullar base Class
		
		$file_in = "<?php\n";
		$file_in .= $this->Copyright();
		$file_in .= "\nrequire_once 'DataBaseMysql.class.php';\n";
		$file_in .="\nClass " . $this->m_strTableName . " {\n\n";	

		$file_in .="	public function  __construct() {
		\$this->connection = new DataBaseMysql();
	}\n\n";
		
		$file_in .= "	\n	/*******************************************************************************************
	******************************** Fetch Functions *******************************************
	*******************************************************************************************/\n";
		
		if($this->fetchData) $file_in = $this->CreateFunctionFetch( $file_in );
		
		if($this->Order_Keys) $file_in = $this->CreateFunctionGetKeysOrder( $file_in );
		
		$file_in = $this->CreateFunctionCloseSQL( $file_in );
			
		$file_in .= "\n}";
		
		$this->SaveFile( $this->m_strTableName . ".class.php", $file_in );
	}

	private function CreateFunctionGetKeysOrder($file){
		$file .="\n	public function GetKeysOrderBy(\$column, \$order){\n";
		$file .="		\$keys = array(); \$i = 0;\n";
		$file .="		\$result = \$this->connection->RunQuery(\"SELECT ".$this->classe_variables[$this->GetKeyOf_table()]['var_name']." from ".$this->classe_name[$this->num_of_tables]['name']." order by \$column \$order\");\n";
		$file .="			while(\$row = \$result->fetch_array(MYSQLI_ASSOC)){\n";
		$file .="				\$keys[\$i] = \$row[\"".$this->classe_variables[$this->GetKeyOf_table()]['var_name']."\"];\n";
		$file .="				\$i++;\n";
		$file .="			}\n";
		$file .="	return \$keys;\n";
		$file .="	}\n";
		return $file;
	}
	
	private function CreateFunctionGetters($file){
		for($i=0; $i!= $this->classe_name[$this->num_of_tables]['total_vars']; $i++ ) {

			$strVarName = $this->classe_variables[$i]['var_name'];
				
			if( true == is_numeric( strrpos( $strVarName, '_' ) ) ) {
				$arrstrVarName = array();
				$arrstrVarName = explode( '_', $strVarName );
				$strVarNameTemp = '';
				foreach( $arrstrVarName as $strVarNameSeprate ) {
					$strVarNameTemp .= ucfirst( $strVarNameSeprate );
				}
				$strVarName = $strVarNameTemp;
			}
			$strVarType 	= $this->classe_variables[$i]['lenght'];
			
			if( true == is_numeric( strrpos( $strVarType, '(' ) ) ) {
				$strVarType = substr( $strVarType, 0, strrpos( $strVarType, '(' ) );
			}
			
			if( 'varchar' == $strVarType || 'text' == $strVarType || 'datetime' == $strVarType || 'timestamp' == $strVarType ) $strVarType = 'str';
			
			$file .= "\n	public function get" . ucfirst( $strVarName ) . "(){\n";
			$file .= "		return \$this->m_" . $strVarType .  ucfirst( $strVarName ) . ";\n";
			$file .= "	}\n";
		}
		return $file;
	}

	private function CreateFunctionSetters($file){
		for($i=0; $i!= $this->classe_name[$this->num_of_tables]['total_vars']; $i++){
			if($this->classe_variables[$i]['var_name'] != 1) {
			
				$strVarName = $this->classe_variables[$i]['var_name'];
				
				if( true == is_numeric( strrpos( $strVarName, '_' ) ) ) {
					$arrstrVarName = array();
					$arrstrVarName = explode( '_', $strVarName );
					$strVarNameTemp = '';
					foreach( $arrstrVarName as $strVarNameSeprate ) {
						$strVarNameTemp .= ucfirst( $strVarNameSeprate );
					}
					$strVarName = $strVarNameTemp;
				}
				
				$strVarType 	= $this->classe_variables[$i]['lenght'];
				
				if( true == is_numeric( strrpos( $strVarType, '(' ) ) ) {
					$strVarType = substr( $strVarType, 0, strrpos( $strVarType, '(' ) );
				}
				
				if( 'varchar' == $strVarType || 'text' == $strVarType || 'datetime' == $strVarType || 'timestamp' == $strVarType ) $strVarType = 'str';
				
				$file .= "\n	public function set" . ucfirst( $strVarName ) . "( $" . ucfirst( $strVarName ) . " ){\n";
				$file .= "		\$this->m_" . $strVarType . ucfirst( $strVarName ) . " = $" . ucfirst( $strVarName ) . ";\n";
				$file .= "	}\n";
			}
		}
		return $file;
	}

	private function CreateFunctionCloseSQL($file){

		$file .= "\n	public function end" . $this->m_strTableName . "(){\n";
		$file .= "		\$this->connection->CloseMysql();\n";
		$file .= "	}\n";
		return $file;
	}

	private function CreateFunctionInsert( $file_in ) {
		$file_in .= "\n	public function insert() {\n";
		$file_in .= "		return \$this->connection->RunQuery( \"Insert into " . strtoupper( $this->classe_name[$this->num_of_tables]['name'] ) . " (";

		for( $i=0; $i!= $this->classe_name[$this->num_of_tables]['total_vars']; $i++ ) {
			if( $this->classe_variables[$i]['key'] != 1 ) {
				$file_in .= $this->classe_variables[$i]['var_name'];
					if( $i+1 != $this->classe_name[$this->num_of_tables]['total_vars'] ) {
						$file_in .=", ";
					}
			}
		}
		$file_in .= ") values (";
		for($i=0; $i!= $this->classe_name[$this->num_of_tables]['total_vars']; $i++){
			if( $this->classe_variables[$i]['key'] != 1 ) {
				if( strtolower( 'created_on' ) == strtolower( $this->classe_variables[$i]['var_name'] ) ) {
					$file_in .= "now() ";
					if($i+1 != $this->classe_name[$this->num_of_tables]['total_vars']){
						$file_in .=", ";
					}
					continue;
				}

				if( strtolower( 'updated_on' ) == strtolower( $this->classe_variables[$i]['var_name'] ) ) {
					$file_in .= "NULL ";
					if($i+1 != $this->classe_name[$this->num_of_tables]['total_vars']){
						$file_in .=", ";
					}
					continue;
				}

				$strVarType 	= $this->classe_variables[$i]['lenght'];

				if( true == is_numeric( strrpos( $strVarType, '(' ) ) ) {
					$strVarType = substr( $strVarType, 0, strrpos( $strVarType, '(' ) );
				}

				$strVarName = ucfirst( $this->classe_variables[$i]['var_name'] );

				if( true == is_numeric( strrpos( $strVarName, '_' ) ) ) {
					$arrstrVarName = array();
					$arrstrVarName = explode( '_', $strVarName );
					$strVarNameTemp = '';
					foreach( $arrstrVarName as $strVarNameSeprate ) {
						$strVarNameTemp .= ucfirst( $strVarNameSeprate );
					}
					$strVarName = $strVarNameTemp;
				}

				if( 'varchar' == $strVarType || 'text' == $strVarType || 'datetime' == $strVarType ) {
					$strFieldValue = "'\"" . " . \$this->get" . $strVarName . "() . \"'";
				} else {
					$strFieldValue = "\"" . " . \$this->get" . $strVarName . "() . \"";
				}

				$file_in .= $strFieldValue;
					if($i+1 != $this->classe_name[$this->num_of_tables]['total_vars']){
						$file_in .=", ";
					}
			}
		}
		$file_in .=")\");\n";
		$file_in .= "	}\n";
		return $file_in;
	}

	private function CreateFunctionUpdate($file_in){
		$file_in .= "\n	public function update() {\n";
		$file_in .= "		\$this->connection->RunQuery(\"UPDATE ".$this->classe_name[$this->num_of_tables]['name']." set ";
		for($i=0; $i!= $this->classe_name[$this->num_of_tables]['total_vars']; $i++){
			if($this->classe_variables[$i]['key'] != 1){
				$file_in .= $this->classe_variables[$i]['var_name']." = \\\"\$this->".$this->classe_variables[$i]['var_name']."\\\"";
					if($i+1 != $this->classe_name[$this->num_of_tables]['total_vars']){
						$file_in .=", ";
					}
			}	
		}
		$file_in .=" . \"where ".$this->classe_variables[$this->GetKeyOf_table()]['var_name']." = \\\"\$this->".$this->classe_variables[$this->GetKeyOf_table()]['var_name']."\\\"\");\n";	
		$file_in .= "	}\n";
		return $file_in;
	}
	
	
	private function CreateFunctionDelete( $file_in ) {
		$file_in .= "\n	public function Delete(\$intId ) {\n";
		$file_in .= "		\$this->connection->RunQuery( \"DELETE FROM " . $this->classe_name[$this->num_of_tables]['name'] . " WHERE " . $this->classe_variables[$this->GetKeyOf_table()]['var_name'] . " = \$intId\");\n";
		$file_in .= "	}\n";	
		return $file_in;
	}
	
	private function CreateFunctionFetchNextId( $file_in ) {
		$file_in .= "\n	public function fetchNextId() {\n";
		$file_in .= "		\$result = \$this->connection->RunQuery( \"SELECT COALESCE( max( id ) + 1 , 1) as id FROM " . $this->classe_name[$this->num_of_tables]['name'] . "\" );\n";
		$file_in .= "		if( false == \$result ) return NULL;\n\n";
		$file_in .= "		while( \$row = \$result->fetch_array( MYSQLI_ASSOC ) ) {\n";
		$file_in .= "			\$this->id = \$row[\"id\"];\n";
		$file_in .= "		}\n\n";
		$file_in .= "		return \$this->id;\n";
		$file_in .= "	}\n";
		return $file_in;
	}
	
	
	private function CreateFunctionFetch( $file_in ) {
			$key_name = $this->classe_variables[$this->GetKeyOf_table()]['var_name'];

				$file_in .="\n	public function fetchAll" . $this->m_strTableName . "() {\n";
				$file_in .= "		\$result = \$this->connection->RunQuery(\"SELECT * FROM " . strtoupper( $this->classe_name[$this->num_of_tables]['name'] ) . "\");\n";
				$file_in .= "		". "$" . "arrobj" . $this->m_strTableName . " = array();\n";
				$file_in .= "		while(\$row = \$result->fetch_array( MYSQLI_ASSOC ) ) {\n";

				for( $k=0; $k!= $this->classe_name[$this->num_of_tables]['total_vars']; $k++ ) {
				 	$file_in .="			\$this->".$this->classe_variables[$k]['var_name']." = \$row[\"".$this->classe_variables[$k]['var_name']."\"];\n";
				}
				
				$file_in .= "			array_push( ". "$" . "arrobj" . $this->m_strTableName . ", ". "$" . "this );\n";
				$file_in .="		}\n" ;
				$file_in .= "		return " . "$" . "arrobj" . $this->m_strTableName . ";";
				$file_in .="\n	}" ;
				
				// Start to create new Fetch Function as per the Parameters
				$file_in .="\n	public function fetchAll" . $this->m_strTableName . "ByIds( \$arrintIds ) {\n";
				$file_in .= "		\$result = \$this->connection->RunQuery(\"SELECT * FROM " . strtoupper( $this->classe_name[$this->num_of_tables]['name'] ) . " WHERE id IN ( implode( " . "\",\"" . " \$arrintIds ) \" );\n";
				$file_in .= "		". "$" . "arrobj" . $this->m_strTableName . " = array();\n";
				$file_in .= "		while( \$row = \$result->fetch_array( MYSQLI_ASSOC ) ) {\n";

				for( $k=0; $k!= $this->classe_name[$this->num_of_tables]['total_vars']; $k++ ) {
				 	$file_in .="			\$this->".$this->classe_variables[$k]['var_name']." = \$row[\"".$this->classe_variables[$k]['var_name']."\"];\n";
				}
				
				$file_in .= "			array_push( ". "$" . "arrobj" . $this->m_strTableName . ", ". "$" . "this );\n";
				$file_in .="		}\n" ;
				$file_in .= "		return " . "$" . "arrobj" . $this->m_strTableName . ";";
				$file_in .="\n	}\n";
	return $file_in;			
	}
	
	
	private function CreateVars( $file ) {
		$this->seters == true ? $type = "protected" : $type = "public";
	
		$m = '$m_';
		for( $i = 0; $i != $this->classe_name[$this->num_of_tables]['total_vars']; $i++ ) {
			
			$strVarType 	= $this->classe_variables[$i]['lenght'];
			$strVarName 	= $this->classe_variables[$i]['var_name'];
			
			if( true == is_numeric( strrpos( $strVarName, '_' ) ) ) {
				$arrstrVarName = array();
				$arrstrVarName = explode( '_', $strVarName );
				$strVarNameTemp = '';
				foreach( $arrstrVarName as $strVarNameSeprate ) {
					$strVarNameTemp .= ucfirst( $strVarNameSeprate );
				}
				$strVarName = $strVarNameTemp;
			}
			
			if( true == is_numeric( strrpos( $strVarType, '(' ) ) ) {
				$strVarType = substr( $strVarType, 0, strrpos( $strVarType, '(' ) );
			}
			
			if( 'varchar' == $strVarType || 'text' == $strVarType || 'datetime' == $strVarType || 'timestamp' == $strVarType ) $strVarType = 'str';
			
			array_push( $this->m_arrstrDataType, array( ucfirst( $strVarName ), $strVarType ) );
			
			$file .= "\t$type $m" . $strVarType . ucfirst( $strVarName ) . ";\n";	
		}
		$file .= "	$type \$connection;\n\n";		
		return $file;
	}
	
	private function GetKeyOf_table(){
		for($z = 0; $z != $this->classe_name[$this->num_of_tables]['total_vars']; $z++){
			if($this->classe_variables[$z]['key'] == 1) return $z;
		}
		return 0;
	}
	
	
	private function CreateDataBaseClass(){
$file = "<?php\n
".$this->Copyright()."\n		
Class DataBaseMysql {\n
	public \$conn;\n
	public function DataBaseMysql(){
		\$this->conn = new mysqli(\"".$this->host."\", \"".$this->user."\", \"".$this->pass."\", \"".$this->dbname."\");
		if(\$this->conn->connect_error){
			echo \"Error connect to mysql\";die;
		}
	}
	
	public function RunQuery(\$query_tag){
		\$result = \$this->conn->query(\$query_tag) or die(\"Erro SQL query-> \$query_tag  \". mysql_error());
		return \$result;
	}

	public function TotalOfRows(\$table_name){
		\$result = \$this->RunQuery(\"Select * from \$table_name\");
		return \$result->num_rows;
	}

	public function CloseMysql(){
		\$this->conn->close();
	}

}

?>"; 
		$this->SaveFile( "DataBaseMysql.class.php", $file );
	}

	private function SaveFile( $filename, $text ) {
		if( $this->VerifyDirectory() ){
			$file = fopen( "classes/" . $filename, "w" );
			fwrite( $file, $text );
			fclose( $file );
		}
	}
	
	private function SaveBaseFile( $filename, $text ) {
		if( $this->VerifyBaseDirectory() ){
			$file = fopen( "classes/Base/" . $filename, "w" );
			fwrite( $file, $text );
			fclose( $file );
		}
	}
	
	private function VerifyDirectory(){
		if( is_dir( "classes" ) ) {
			return true;
		}else{
			mkdir( "classes", "0777", true );
			return true;
		}
	}
	
	private function VerifyBaseDirectory(){
		if( is_dir( "classes/Base" ) ) {
			return true;
		}else{
			mkdir( "classes/Base", "0777", true );
			return true;
		}
	}
	
	private function Copyright(){
		return "/*
 * Author:  Somnath Shinde- soma.shinde@gmail.com Mobile No:- 9423792564
 * 
 * Create Date: ".date("j-m-Y")."
 * 
 * Version of MYSQL_to_PHP: 1.1
 * 
 * Ranzani( YZ'S );
 * 
 */";
	}

	public function getNum_of_tables() {
		return $this->num_of_tables;
	}

	public function getNum_of_total_variables() {
		return $this->num_of_total_variables;
	}
	

	public function setGeters($geters) {
		$this->geters = $geters;
	}
	

	public function setfetchData($fetchData) {
		$this->fetchData = $fetchData;
	}
	

	public function setupdate($update) {
		$this->update = $update;
	}
	

	public function setinsert($insert) {
		$this->insert = $insert;
	}

	public function setSeters($seters) {
		$this->seters = $seters;
	}

	public function getClasse_name() {
		return $this->classe_name;
	}

	public function setConstruct($construct) {
		$this->construct = $construct;
	}

	public function setdelete($delete) {
		$this->delete = $delete;
	}

	public function setOrder_Keys($Order_Keys) {
		$this->Order_Keys = $Order_Keys;
	}


	public function endMySQL_to_PHP(){
		$this->connection->close();
	}
	
}

function display( $strData ) {
	print_r( '<pre>' );
	print_r( $strData );
	print_r( '</pre>' );
}

?>